package com.pets.springboot_petshop.service.impl;

import com.pets.springboot_petshop.entity.Admin;
import com.pets.springboot_petshop.entity.AdminDTO;
import com.pets.springboot_petshop.mapper.AdminMapper;
import com.pets.springboot_petshop.service.AdminService;
import com.pets.springboot_petshop.util.Md5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
 * @Author: wxk
 * @Email: 2089967794@qq.com
 * @Date: 2024/10/8 上午9:44 周二
 * @Description: 管理员
 */
@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper adminMapper;

    @Override
    public Admin select(int id) {
        return adminMapper.select(id);
    }

    @Override
    public Admin findByAdminName(String username) {
        Admin admin = adminMapper.findByAdminName(username);
        return admin;
    }

    @Override
    public Admin findByAdminemail(String email) {
        Admin admin = adminMapper.findByAdminemail(email);
        return admin;
    }

    @Override
    public void addAdmin(String username, String password,String role,  String email) {
        //加密
        String md5String = Md5Util.getMD5String(password);
        //添加
        adminMapper.addAdmin(username,md5String,role,email);
    }

    @Override
    public List<Admin> findAll() {
        return adminMapper.findAll();
    }

//    @Override
//    public int updateAdminpassword(Admin admin) {
//        String password = admin.getPassword();
//        String md5String = Md5Util.getMD5String(password);
//        admin.setPassword(md5String);
//        return adminMapper.updateAdminpassword(admin);
//    }

    @Override
    public int updateAdminById(Admin admin) {
        return adminMapper.updateAdminById(admin);
    }


    @Override
    public int deleteAdminById(int id) {
        return adminMapper.deleteAdminById(id);
    }

    @Transactional
    public boolean updateAdminpassword(Admin admin) {
        if (admin == null || admin.getEmail() == null || admin.getPassword() == null) {
            System.out.println("密码修改成功2:  " +admin);
            return false;
        }
        String encryptedPassword = Md5Util.getMD5String(admin.getPassword());
        admin.setPassword(encryptedPassword);
        return adminMapper.updateAdminpassword(admin) > 0;
    }

    @Override
    public AdminDTO convertToDTO(Admin admin) {
        if (admin == null) {
            return null;
        }
        AdminDTO adminDTO = new AdminDTO();
        adminDTO.setId(admin.getId());
        adminDTO.setUsername(admin.getUsername());
        adminDTO.setEmail(admin.getEmail());
        adminDTO.setCreate_time(admin.getCreate_time());
        adminDTO.setUpdate_time(admin.getUpdate_time());
        adminDTO.setRole(admin.getRole());
        return adminDTO;
    }

    @Override
    public List<Admin> selectusername(String username) {
        return adminMapper.selectusername(username);
    }

    @Override
    public List<Admin> selectemail(String email) {
        return adminMapper.selectemail(email);
    }
}
